package com.school.movie.common.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

/**
 * @author swift
 */
public class FfmpegUtil {

    private static final String fFmpegPath = "D:\\webkit\\ffmpeg-4.3.1\\ffmpeg-4.3.1-2021-01-26-full_build\\bin\\";
    private static final String TOOLS_FFMPEG = "ffmpeg";

    public static boolean generateVideoCoverImage(String videoFile, String imgFile, String xtime, String pix) {
        List<String> commend = new ArrayList<>();
        commend.add(fFmpegPath + TOOLS_FFMPEG);
        commend.add("-i");
        commend.add(videoFile);
        commend.add("-y");
        commend.add("-f");
        commend.add("mjpeg");
        commend.add("-ss");
        commend.add(xtime);
        commend.add("-t");
        commend.add("0.001");
        commend.add("-s");
        commend.add(pix);
        commend.add(imgFile);
        try {
            ProcessBuilder builder = new ProcessBuilder();
            Process proc = builder.command(commend).redirectErrorStream(true).start();
            InputStream stderr = proc.getErrorStream();
            InputStreamReader isr =  new InputStreamReader(stderr);
            BufferedReader br = new BufferedReader(isr);
            String line = null;
            while((line = br.readLine()) != null){
                System.err.println(line);
            }
            int exitVal = proc.waitFor();
            System.out.println("exitCode" + exitVal);
        } catch (InterruptedException | IOException e) {
            e.printStackTrace();
        }
        return true;
    }


}
